{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c6cb345b-d2a1-408f-8e91-e562f5cd032a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "* running on cpu\n"
     ]
    }
   ],
   "source": [
    "########################################################################################################\n",
    "# The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM\n",
    "########################################################################################################\n",
    "\n",
    "import numpy as np\n",
    "import types\n",
    "import copy\n",
    "import torch\n",
    "from torch.nn import functional as F\n",
    "\n",
    "from model import RWKV_RNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "70ff5b46-9362-47e9-a1b4-26cac9698c69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "******************************************************************************\n",
      "* This is a preview of RWKV-v2-RNN trained on the Pile for only 50B tokens.\n",
      "* It is NOT indicative of the final performance (which requires 300B tokens).\n",
      "******************************************************************************\n"
     ]
    }
   ],
   "source": [
    "np.set_printoptions(precision=4, suppress=True, linewidth=200)\n",
    "\n",
    "print('''\n",
    "******************************************************************************\n",
    "* This is a preview of RWKV-v2-RNN trained on the Pile for only 50B tokens.\n",
    "* It is NOT indicative of the final performance (which requires 300B tokens).\n",
    "******************************************************************************''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "795e2228-cc19-4e57-bcb7-5027365519c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Edit model.py to set CPU / CUDA mode. Runs on CPU by default.\n",
    "\n",
    "TEMPERATURE = 1.0\n",
    "TOP_P = 0.7\n",
    "\n",
    "DEBUG_DEBUG = False\n",
    "LENGTH_OF_EACH = 333\n",
    "NUM_TRIALS = 100\n",
    "\n",
    "context = '\\nDataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence.'\n",
    "\n",
    "##############################################################################################################"
   ]
  },
  {
   "cell_type": "raw",
   "id": "08295fac-a5ce-478f-8bd9-fc191aae1af0",
   "metadata": {},
   "source": [
    "模型下载地址：https://hf-mirror.com/BlinkDL/rwkv-2-pile-430m/resolve/main/20220615-10803.pth?download=true"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f41497aa-4bff-4a91-8e1e-d811b3077170",
   "metadata": {},
   "source": [
    "请在model.py中修改模型路径\n",
    "例如我的路径是/data1/ckw/20220615-10803\n",
    "如果想使用cuda加速，请参考：https://github.com/BlinkDL/RWKV-v2-RNN-Pile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "329c7001-102a-4d84-88ba-2b97a68c9557",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "loading RWKV-RNN /data1/ckw/20220615-10803\n"
     ]
    }
   ],
   "source": [
    "model = RWKV_RNN()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "326e1bc0-eaa2-4f4e-94d9-60fe99d8768f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_logits(out, temperature=1.0, top_p=None):\n",
    "    probs = F.softmax(torch.tensor(out), dim=-1)\n",
    "    sorted_probs, _ = torch.sort(probs, descending=True)\n",
    "\n",
    "    cumulative_probs = torch.cumsum(sorted_probs, dim=-1).numpy()\n",
    "    cutoff = float(sorted_probs[np.argmax(cumulative_probs > top_p)])\n",
    "    probs[probs < cutoff] = 0\n",
    "\n",
    "    if temperature != 1.0:\n",
    "        probs = probs.pow(1.0 / temperature)\n",
    "\n",
    "    return torch.multinomial(probs, num_samples=1)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "392a71df-ac9e-4349-a60f-8988466e5812",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. We train artificial intelligence and robotics experts from around the world to build innovative artificial intelligence systems for education. We also develop software and systems to improve the quality of data for AI and robotics. DataWhalechina provides learning solutions for AI and robotics, software development and data analysis. We help AI developers to create new applications and models, using machine learning to understand the human brain and to improve its performance.\n",
      "\n",
      "Our goal is to build AI systems for educational purposes. We are an international AI organization, with over 20 years of experience in training AI specialists. Our expertise is based on the following:\n",
      "\n",
      "Advantages\n",
      "\n",
      "Maintain high standards of quality\n",
      "\n",
      "Create effective learning opportunities\n",
      "\n",
      "Ensure the reliability of data\n",
      "\n",
      "Help people in training\n",
      "\n",
      "Reduce costs\n",
      "\n",
      "Expand the reach of the organization\n",
      "\n",
      "We’re proud to be the first organization to create an AI that focuses on data quality.\n",
      "\n",
      "What is a data quality assessment?\n",
      "\n",
      "Data quality assessment is a process of conducting quality assessment of data. Data quality assessment uses techniques that examine the process of data collection and analysis, to assess the quality of the data collected.\n",
      "\n",
      "If the process of data quality assessment is not complete, the data will be deleted from the system.\n",
      "\n",
      "If the process of data quality assessment is not complete, the system will be subject to further analysis and processing.\n",
      "\n",
      "The data quality assessment will also include an analysis of the system to determine whether the data quality assessment has been completed.\n",
      "\n",
      "How does data quality assessment work?\n",
      "\n",
      "When an organization works on a data quality assessment, it\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. The organization started as an experimental project. They have been working on different projects and one of them is datawhale.io.\n",
      "\n",
      "At the time of writing, DataWhale is being used by schools and institutions to help students understand data science and to train their algorithms.\n",
      "\n",
      "DataWhale has many projects and they have been active since 2018. There are over 10 projects and thousands of datawhale.io users on their network.\n",
      "\n",
      "DataWhale.io has been working on different projects. They started with the open source project ZopSketch, that helps create a smart camera and it’s still in development. They have also been working on AutoLink, which allows users to automatically upload a link to their own photo on social media.\n",
      "\n",
      "They have been working on Tekken, which is a competitive action game and they have developed the program MonsterJigsaw. They have also been working on the UBIO game.\n",
      "\n",
      "DataWhale is helping people to improve their data science skills, especially by providing tools that can help them with their own data.\n",
      "\n",
      "DataWhale has also been working on data science projects that help data scientists and data scientists. DataWhale has been working on various data science projects. They have created tools to help companies, organizations, and businesses use data.\n",
      "\n",
      "DataWhale.io is a data science company that has created data science projects.\n",
      "\n",
      "DataWhale.io has worked on various projects, including data science and data science. They have created data science projects that help people improve their data science skills.\n",
      "\n",
      "DataWhale has been working\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. The group trains students in the language, skill and design of artificial intelligence and research in AI. It was created in 2018 and is currently in operation.\n",
      "\n",
      "International Students\n",
      "\n",
      "Learn about our international students and international students that are currently enrolled at DataWhalechina.\n",
      "\n",
      "Tangxia Online\n",
      "\n",
      "Tangxia Online is a student portal for all Chinese students in Shanghai. It enables them to see information about Shanghai, access information about current courses and news about Shanghai.\n",
      "\n",
      "China Business China (CBBC)\n",
      "\n",
      "CBBC is a portal that hosts global business courses, online courses and information about China in Shanghai.\n",
      "\n",
      "As the global host of the international online program CBBC, DataWhalechina has established a network of Chinese students, alumni, educators and business students. The portal offers a number of valuable courses and information.\n",
      "\n",
      "About DataWhalechina\n",
      "\n",
      "DataWhalechina is a Chinese portal that hosts data and information about international students. The portal provides information about current courses and information about China in Shanghai.\n",
      "\n",
      "DataWhalechina.com is a portal that allows users to view courses, course options, materials and information about Shanghai.\n",
      "\n",
      "DataWhalechina.com has a strong focus on providing students with online resources and information about Shanghai, China.\n",
      "\n",
      "For more information, visit datawhalechina.com.\n",
      "\n",
      "China Data China\n",
      "\n",
      "China Data China is a portal that provides users with online information and resources about Shanghai, China. The portal provides users with information about Shanghai, China.\n",
      "\n",
      "DataWhalechina is a portal that allows\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. They have trained over 2,000 people to date, and are now one of the top 10 data mining companies in the world.\n",
      "\n",
      "This means they can offer a variety of training programs to their users. These include data mining and artificial intelligence course. They have trained people in artificial intelligence and machine learning. They have also trained an AI-powered library of services. This means they can help any company that wants to take advantage of AI-powered data mining services.\n",
      "\n",
      "Founded in 2017, DataWhalechina is a machine learning and artificial intelligence company based in Shanghai, China. They have trained some of the world’s top 5 AI-powered data mining companies. Their services include machine learning, artificial intelligence, and data mining.\n",
      "\n",
      "DataWhalechina has been working in the AI industry for many years. They have worked on many projects and technologies, including AI-powered platforms.\n",
      "\n",
      "DataWhalechina is a member of the Information and Technology Sector (IT) Council.\n",
      "\n",
      "The company also works with startups to create AI-powered services that will help their clients.\n",
      "\n",
      "These services include machine learning and artificial intelligence training. They also have many consultants that are trained on the company’s platform.\n",
      "\n",
      "The company also has a data scientist, an artificial intelligence researcher, and a data science engineer.\n",
      "\n",
      "DataWhalechina has been in the AI industry for more than 20 years. They have an office in Shanghai, China.\n",
      "\n",
      "The company offers many different services to help their clients learn AI. They have courses and workshops.\n",
      "\n",
      "The company has worked with startups that have worked\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. We have a strong commitment to developing learning tools for everyone. DataWhalechina’s AI and ML platform offers users a free course on ML algorithms and data mining for anyone with basic programming skills.\n",
      "\n",
      "We will start with a small introduction to Machine Learning, we will start with building the first model and the first algorithm, followed by a few more.\n",
      "\n",
      "Data mining\n",
      "\n",
      "Machine learning is a process of classifying data into classes based on the principles of statistical inference and machine learning. The purpose of machine learning is to classify data into different categories.\n",
      "\n",
      "Data mining is the science of extracting useful information from large amounts of data.\n",
      "\n",
      "You can think of data mining as a way to find patterns in data. For example, if you’re looking for the average temperature in a certain area, you can try to classify data based on temperature.\n",
      "\n",
      "Data mining is also a technique that uses machine learning to predict the future behaviour of an object.\n",
      "\n",
      "Machine learning can also be used to predict the future behaviour of a system. This is called a model-based learning algorithm.\n",
      "\n",
      "Data mining is a relatively new discipline, and many people haven’t even heard of it. However, we do have some basic concepts of data mining.\n",
      "\n",
      "Data mining is a technique for creating models to predict future events based on data. Data mining is also known as “machine learning” or “artificial intelligence”.\n",
      "\n",
      "Machine learning can be used to create models for some tasks. For example, an algorithm can be trained to predict a salesman’s behaviour based on past sales.\n",
      "\n",
      "If you want to make a decision\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. DataWhalechina works with universities and the government to provide AI training, including computer-assisted learning, for students in areas like health care, sports, and science.\n",
      "\n",
      "DataWhalechina was founded in 2015.\n",
      "\n",
      "DataWhalechina provides AI research, teaching, and software development for the United States, Europe, and China. It was founded by China University of Political Science and Law, University of Washington, and University of San Diego.\n",
      "\n",
      "Since 2017, DataWhalechina has operated the Web Science Institute. DataWhalechina is also involved in the field of artificial intelligence.\n",
      "\n",
      "List of data scientists\n",
      "DataWhalechina has made numerous contributions to the field of AI, including its founding of the DataScience Initiative, a non-profit organization that conducts research and analysis of artificial intelligence. The company is currently focused on improving the quality of AI research.\n",
      "\n",
      "DataWhalechina has conducted various studies and experiments in the field of artificial intelligence, including one in which data scientists learned how to use machine learning algorithms to predict human emotions.\n",
      "\n",
      "Datawhalechina has been involved in various projects related to artificial intelligence, such as its partnership with Leverage Ventures, an AI startup that focuses on artificial intelligence research.\n",
      "\n",
      "DataWhalechina and Leverage Ventures have collaborated on numerous data science projects, including a project that builds a machine learning model that detects patterns in YouTube videos.\n",
      "\n",
      "DataWhalechina was also involved in helping Leverage Ventures to launch their research into machine learning.\n",
      "\n",
      "DataWhalechina was\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. The organization also is involved in many other non-profit organizations such as the Alumni Association of the University of California, Los Angeles, and the United States Army.\n",
      "\n",
      "History \n",
      "DataWhalechina was founded in May 2018.\n",
      "\n",
      "Awards \n",
      "The organization has been awarded the following awards:\n",
      " 2018: Business Week\n",
      " 2018: Google News\n",
      " 2018: MediaWorld\n",
      "\n",
      "References\n",
      "\n",
      "External links \n",
      " Official website\n",
      "\n",
      "Category:2018 establishments in China\n",
      "Category:Companies based in Shanghai\n",
      "Category:Companies established in 2018\n",
      "Category:Companies based in Guangdong\n",
      "Category:Internet search engines\n",
      "Category:Information and communication technology companies\n",
      "Category:International organizations based in China\n",
      "Category:Internet search engines\n",
      "Category:Organizations established in 2018\n",
      "Category:Information technology organizations based in Europe\n",
      "Category:Non-profit organizations based in Shanghai\n",
      "Category:Organizations that support open access\n",
      "Category:Artificial intelligence organizations\n",
      "Category:Companies based in Shanghai\n",
      "Category:2019 mergers and acquisitions\n",
      "Category:Chinese brands\n",
      "Category:Technology companies of China\n",
      "Category:2019 mergers and acquisitions\n",
      "Category:Technology companies established in 2018\n",
      "Category:2019 mergers and acquisitions\n",
      "Category:Technology companies established in 2018\n",
      "Category:2018 mergers and acquisitions\n",
      "Category:2019 mergers and acquisitions\n",
      "Category:Istanbul Technical University\n",
      "Category:Online learning companies\n",
      "Category:Tech companies of China\n",
      "Category:Technology companies established in 2018\n",
      "Category:Technology companies established in 2019\n",
      "Category:Technology companies of China\n",
      "Category:2018 establishments in China\n",
      "Category:Technology companies of China\n",
      "Category:2018 mergers and\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. Their mission is to help students become more effective learner. They focus on educating learners to master artificial intelligence (AI) and to improve their learning experience. They conduct research, help students learn and improve their skills, and bring their knowledge and understanding to the public.\n",
      "\n",
      "We are all in the same boat. We all know the situation, but no one is doing anything about it. There is no one that can make things better, so how do we fix this?\n",
      "\n",
      "In this article, we are going to share our perspective and ask you to help. We are all in the same boat, so how can we fix this?\n",
      "\n",
      "In this article, we will share our perspective and ask you to help us. We are all in the same boat. We all know the situation, but no one knows the answer.\n",
      "\n",
      "What is AI?\n",
      "\n",
      "An AI is an intelligent program that is capable of understanding the human condition, finding the truth, and making decisions.\n",
      "\n",
      "This term has been in use for the past 20 years and is considered a self-driving car.\n",
      "\n",
      "It is a machine that is capable of learning from experiences and taking actions, and making decisions, based on these actions.\n",
      "\n",
      "It is a self-driving car that is self-learning and learns from experiences. It is an intelligent machine that is able to learn from experiences, and make decisions, based on experiences.\n",
      "\n",
      "Why AI?\n",
      "\n",
      "AI is a method of learning that is being developed in a new way that is unprecedented. This is what makes AI such a promising field to work on.\n",
      "\n",
      "What is an AI?\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. We have a passion for AI and are always working on it. If you want to be an AI expert, join our membership and get access to a huge collection of free courses, resources, videos, tutorials, and more. Join today!\n",
      "\n",
      "https://twitter.com/DataWhalechina\n",
      "\n",
      "https://www.facebook.com/DataWhalechina\n",
      "\n",
      "https://www.instagram.com/DataWhalechina\n",
      "\n",
      "https://www.linkedin.com/company/data-whale\n",
      "\n",
      "Join our platform and learn the benefits of data.\n",
      "\n",
      "Get Data Whale\n",
      "\n",
      "Delve into the data world with the help of this professional engineer. This man can design the biggest robot, program the smartest AI in the world, and design the best AI in the world. It’s easy to learn and fun to watch. You can also use this platform to create your own AI projects, and join our community to be an expert in AI.\n",
      "\n",
      "https://diveintodatawhale.com/\n",
      "\n",
      "https://www.linkedin.com/in/diveintodatawhale\n",
      "\n",
      "https://linkedin.com/in/diveintodatawhale\n",
      "\n",
      "www.linkedin.com/in/diveintodatawhale\n",
      "\n",
      "www.linkedin.com/in/davewhale\n",
      "\n",
      "Dive Into Data Whale\n",
      "\n",
      "This is the latest free data whitepaper. We use data from datawhalechina.com to build our AI and data-driven products.\n",
      "\n",
      "Data Whale\n",
      "\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. Its mission is to help AI learners in the UK by providing them with free training.\n",
      "\n",
      "DataWhalechina was created by the Open Data Institute in the UK. It was launched on March 19, 2017, and it provides online courses for AI professionals to improve their skills in data science and data analytics. The program runs in the UK on an annual basis.\n",
      "\n",
      "The O’Reilly books \n",
      "\n",
      "In 2017, DataWhalechina launched a new book titled Data Whale: How the Big Data Industry is Changing the Way We Think and Act. It contains four parts, each part dealing with a different aspect of data science. The first part is a \"standard-answer\" chapter that explains how to make sense of the massive data available to you. The second part is a \"how-to\" guide that discusses the four parts, and the third part contains a chapter titled \"Big Data Analytics\". The fourth part of the book deals with analytics tools, like Amazon Web Services, the AWS cloud, and Apache Kafka.\n",
      "\n",
      "Other chapters include:\n",
      "\n",
      "A guide to artificial intelligence\n",
      "A guide to data science\n",
      "An introduction to machine learning\n",
      "An introduction to data science\n",
      "An introduction to data visualization\n",
      "\n",
      "The book has sections on machine learning, artificial intelligence, data science, artificial intelligence, and related fields. The book also includes a section on artificial intelligence basics, including data mining, machine learning, and machine learning in medicine.\n",
      "\n",
      "References\n",
      "\n",
      "External links \n",
      " DataWhalechina\n",
      "\n",
      "Category:Software companies of the United Kingdom\n",
      "Category:Software companies of the United States\n",
      "Category:Data science\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence.\n",
      "\n",
      "“The learning process will be more structured and easier,” said Swinoujci. “It will be a more enjoyable experience for students, which will in turn lead to better learning experiences.”\n",
      "\n",
      "In recent years, the industry has seen a significant uptick in the number of artificial intelligence (AI) training courses offered by companies. This includes instructional courses such as Artificial Intelligence in Banking and Finance, AI in Construction and Finance, AI in Energy, and AI in Hospitality and Leisure.\n",
      "\n",
      "AI is a topic that is well suited to the artificial intelligence field, as it offers an immersive learning experience that requires sophisticated knowledge. This is why artificial intelligence is increasingly being used in training courses.\n",
      "\n",
      "“AI in training courses is becoming increasingly popular in the industry,” said Swinoujci. “AI is transforming the way we teach artificial intelligence.”\n",
      "\n",
      "Swinoujci said that artificial intelligence in training courses will be “extremely challenging,” but he added that he is optimistic that the AI course will soon become a more accessible form of learning.\n",
      "\n",
      "“AI in training courses is a well-organized, structured, and challenging course that can be integrated into the education of future engineers,” he said. “I think it is crucial for the field of artificial intelligence to become more widely accessible, and that it can be used as a source of training.”\n",
      "\n",
      "Chinese investors are highly enthusiastic about artificial intelligence. This comes as a huge boon for the sector. The market is estimated to grow at an annual rate of over $11 billion in the next three years, and this will drive the industry\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. They specialize in developing AI and artificial intelligence in China.\n",
      "\n",
      "In this video, I was asked about the future of AI in China. In this video, I share my story about how I got my job and how I feel about AI today.\n",
      "\n",
      "The case for AI\n",
      "\n",
      "When you think about AI, you think about machines, but how does AI actually help humans? I have two main ideas about AI.\n",
      "\n",
      "1. AI allows humans to do amazing things\n",
      "\n",
      "AI can do incredible things. For example, artificial intelligence can do amazing things for machine learning. In the case of this video, I used a machine learning algorithm to detect suspicious behavior from millions of people. The algorithm was so good, that the machine was able to catch some very interesting information. This is what we have to take away from this story.\n",
      "\n",
      "2. AI allows us to see the world\n",
      "\n",
      "I was taught by a Chinese man who told me how AI could help us see the world in a new way. I learned a lot about the human mind from AI. I saw how machines can make decisions based on what people see. I also saw how machines can understand the natural world better than humans.\n",
      "\n",
      "I believe AI can make the world a better place. In the future, AI will become a fundamental part of how we think about the world.\n",
      "\n",
      "How AI can help you\n",
      "\n",
      "Here are some things you can do to help us make the world a better place:\n",
      "\n",
      "Make your computer smarter\n",
      "\n",
      "In the next lesson, I will show you how AI can make your computer smarter. I want you to use this lesson\n",
      "----------------------------------------------------------------------\n",
      "DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. We’re also building a team of Chinese artificial intelligence experts to help train AI researchers. We believe AI is the next frontier for education.\n",
      "\n",
      "Can you tell us about your organization?\n",
      "\n",
      "DataWh"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[6], line 31\u001b[0m\n\u001b[1;32m     29\u001b[0m     out \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(init_state\u001b[38;5;241m.\u001b[39mout)\n\u001b[1;32m     30\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 31\u001b[0m     out \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     33\u001b[0m out[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m999999999\u001b[39m  \u001b[38;5;66;03m# disable <|endoftext|>\u001b[39;00m\n\u001b[1;32m     35\u001b[0m char \u001b[38;5;241m=\u001b[39m sample_logits(out, temperature\u001b[38;5;241m=\u001b[39mTEMPERATURE, top_p\u001b[38;5;241m=\u001b[39mTOP_P)\n",
      "File \u001b[0;32m/data1/ckw/01大语言模型/rwkv-v2/model.py:249\u001b[0m, in \u001b[0;36mRWKV_RNN.run\u001b[0;34m(self, ctx)\u001b[0m\n\u001b[1;32m    247\u001b[0m     x \u001b[38;5;241m=\u001b[39m x \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mSA(x, w\u001b[38;5;241m.\u001b[39mblocks[i]\u001b[38;5;241m.\u001b[39matt, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124matt.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m    248\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mLN(x, w\u001b[38;5;241m.\u001b[39mblocks[i]\u001b[38;5;241m.\u001b[39mln2)\n\u001b[0;32m--> 249\u001b[0m     x \u001b[38;5;241m=\u001b[39m x \u001b[38;5;241m+\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mFF\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mblocks\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mffn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mffn.\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mi\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m    251\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mLN(x, w\u001b[38;5;241m.\u001b[39mln_out)\n\u001b[1;32m    253\u001b[0m x \u001b[38;5;241m=\u001b[39m w\u001b[38;5;241m.\u001b[39mhead\u001b[38;5;241m.\u001b[39mweight \u001b[38;5;241m@\u001b[39m x\n",
      "File \u001b[0;32m/data1/ckw/01大语言模型/rwkv-v2/model.py:213\u001b[0m, in \u001b[0;36mRWKV_RNN.FF\u001b[0;34m(self, xx, w, name)\u001b[0m\n\u001b[1;32m    210\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mxx[name] \u001b[38;5;241m=\u001b[39m xx\n\u001b[1;32m    212\u001b[0m r \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39msigmoid(w\u001b[38;5;241m.\u001b[39mreceptance\u001b[38;5;241m.\u001b[39mweight \u001b[38;5;241m@\u001b[39m x)\n\u001b[0;32m--> 213\u001b[0m k \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39msquare(\u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrelu\u001b[49m\u001b[43m(\u001b[49m\u001b[43mw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mkey\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mweight\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m@\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m    214\u001b[0m kv \u001b[38;5;241m=\u001b[39m w\u001b[38;5;241m.\u001b[39mvalue\u001b[38;5;241m.\u001b[39mweight \u001b[38;5;241m@\u001b[39m k\n\u001b[1;32m    216\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m r \u001b[38;5;241m*\u001b[39m kv\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "for TRIAL in range(1 if DEBUG_DEBUG else NUM_TRIALS):\n",
    "    ctx = [model.tokenizer.encode(context)][0]\n",
    "    src_len = len(ctx)\n",
    "    print(context, end='')\n",
    "\n",
    "    model.clear()\n",
    "    if TRIAL == 0:\n",
    "        init_state = types.SimpleNamespace()\n",
    "        for i in range(src_len if DEBUG_DEBUG else src_len):\n",
    "            x = ctx[:i+1]\n",
    "            if i == src_len - 1:\n",
    "                init_state.out = model.run(x)\n",
    "            else:\n",
    "                model.run(x)\n",
    "        model.save(init_state)\n",
    "    else:\n",
    "        model.load(init_state)\n",
    "\n",
    "    if DEBUG_DEBUG:\n",
    "        out = init_state.out\n",
    "        print('\\n', np.array(x), '==>', np.array(\n",
    "            out), np.max(out), np.min(out))\n",
    "\n",
    "    for i in range(src_len, src_len + (0 if DEBUG_DEBUG else LENGTH_OF_EACH)):\n",
    "        x = ctx[:i+1]\n",
    "        x = x[-model.ctx_len:]\n",
    "\n",
    "        if i == src_len:\n",
    "            out = copy.deepcopy(init_state.out)\n",
    "        else:\n",
    "            out = model.run(x)\n",
    "\n",
    "        out[0] = -999999999  # disable <|endoftext|>\n",
    "\n",
    "        char = sample_logits(out, temperature=TEMPERATURE, top_p=TOP_P)\n",
    "        char = char.item()\n",
    "        print(model.tokenizer.decode(char), end='', flush=True)\n",
    "\n",
    "        ctx += [char]\n",
    "    print('\\n' + '-' * 70, end='')"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2c54ec05-21ee-4f71-80d8-b028e1c60913",
   "metadata": {},
   "source": [
    "这里，我们以DataWhalechina is an organization founded at Shanghai Jiao Tong University that helps learners learn artificial intelligence. \n",
    "为开头，写了100组，因为太多，我们就看几组效果就行。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
